package com.cby.android.library.remote

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitManager {

    fun <T> getService(service: Class<T>, baseUrl: String): T {

        val retrofit = Retrofit.Builder().baseUrl(baseUrl)
            //格式转换
            .addConverterFactory(GsonConverterFactory.create())
            .client(getHttpClient())
            .build()

        return retrofit.create(service)

    }

    private fun getHttpClient(): OkHttpClient = OkHttpClient.Builder()
        .connectTimeout(7_000L, TimeUnit.MILLISECONDS)
        .readTimeout(10_000, TimeUnit.MILLISECONDS)
        .writeTimeout(30_000, TimeUnit.MILLISECONDS)
        //自定义拦截器用于日志输出
        .addInterceptor(LogInterceptor())
        .build()
}